package com.yhp.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.system.comm.util.JsonUtils;
import com.system.common.pojo.BisNurseProject;
import com.system.common.pojo.BisNurseType;
import com.yhp.service.BisNurseProjectService;
import com.yhp.service.BisNurseTypeService;

@Controller
@RequestMapping("bnp")
public class BisNurseProjectController {
	@Autowired
	private BisNurseProjectService bisNurseProjectService;
	@Autowired
	private BisNurseTypeService bisNurseTypeService;

	/**
	 * 护理项目>>>>>>列表显示所有
	 * 
	 * @return
	 */
	@RequestMapping("project")
	public String bisNurseProjectList(HttpServletRequest request, Model model) {
		Map<String, Object> map = initMap(request);
		List<BisNurseProject> selectByExample = bisNurseProjectService.selectByExample(map);
		model.addAttribute("selectByExample", selectByExample);

		List<BisNurseType> listType = bisNurseTypeService.selectByExample();
		model.addAttribute("listType", listType);

		return "bis_nurse/project";
	}

	public Map<String, Object> initMap(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		String projectId = request.getParameter("projectId");
		String projectName = request.getParameter("projectName");
		String status = request.getParameter("status");
		String typeId = request.getParameter("typeId");
		if (projectId != null && !projectId.equals("")) {
			map.put("projectId", projectId);
		}
		if (projectName != null && !projectName.equals("")) {
			map.put("projectName", projectName);
		}
		if (status != null && !status.equals("")) {
			map.put("status", status);
		}
		if (typeId != null && !typeId.equals("")) {
			map.put("typeId", typeId);
		}
		return map;
	}

	/**
	 * 护理项目>>>>>>添加
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("project_to_add")
	public String project_to_add(BisNurseProject bisNurseProject,@RequestParam("files") MultipartFile[] files,
			HttpServletRequest request,HttpSession session) {
		file(files, request, bisNurseProject);
		bisNurseProjectService.insert(bisNurseProject, session);
		return "redirect:/bnp/project";
	}

	/**
	 * 护理项目>>>>>>查看详情
	 * 
	 * @return
	 */
	@RequestMapping("project_details")
	public String project_details(String projectId, Model model) {
		BisNurseProject bisNurseProject = bisNurseProjectService.selectByPrimaryKey(projectId);
		model.addAttribute("bisNurseProject", bisNurseProject);
		return "bis_nurse/project_details";
	}
	
	/**
	 * 护理项目>>>>>>删除查询
	 * 
	 * @param projectId
	 * @return
	 */
	@RequestMapping(value="project_selectDel", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String project_selectDel(String projectId) {
		boolean state = bisNurseProjectService.selectByProjectId(projectId);
		if (state) {
			return JsonUtils.toJson("ok");
		}
		return JsonUtils.toJson("error");
	}

	/**
	 * 护理项目>>>>>>删除
	 * 
	 * @param projectId
	 * @return
	 */
	@RequestMapping("project_del")
	public String project_del(String projectId, HttpSession session) {
		projectId = projectId.substring(0, projectId.length() - 1);
		BisNurseProject bisNurseProject = bisNurseProjectService.selectByPrimaryKey(projectId);
		if (bisNurseProject != null) {
			bisNurseProject.setIsDel("1");
			bisNurseProjectService.updateByPrimaryKey(bisNurseProject, session);
		}
		return "redirect:/bnp/project";
	}

	/**
	 * 护理项目>>>>>>修改
	 * 
	 * @return
	 */
	@RequestMapping("project_update")
	public String project_update(BisNurseProject bisNurseProject,@RequestParam("files") MultipartFile[] files,
			HttpServletRequest request,HttpSession session) {
		if (files != null) {
			file(files, request, bisNurseProject);
		}
		bisNurseProject.setIsDel("0");
		bisNurseProjectService.updateByPrimaryKey(bisNurseProject, session);
		return "redirect:/bnp/project";
	}

	/**
	 * 文件上传
	 * 
	 * @param files
	 * @param request
	 * @param bisNurseProject
	 */
	public void file(MultipartFile[] files, HttpServletRequest request, BisNurseProject bisNurseProject) {
		if (files != null && files.length > 0) {// 判断file数组不能为空并且长度大于0
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			if (files[0].getOriginalFilename() != null && !files[0].getOriginalFilename().equals("")) {
				@SuppressWarnings("deprecation")
				String path = request.getRealPath("/upload/coverPhoto");// 获取项目的路径
				path = path + "/" + sdf.format(new Date());// 存放图片的路径
				File file2 = new File(path);
				if (!file2.exists()) {// 如果当前项目不存在
					file2.mkdirs();// 创建一个新的空目录
				}
				String fileName1 = files[0].getOriginalFilename();// 获取文件名
				File file3 = new File(path, fileName1);// 第一个参数表示第二个参数所在的路径
				if (!file3.exists()) {// 如果当前项目不存在
					try {
						file3.createNewFile();// 创 建一个空的目录
						files[0].transferTo(file3);// 将上传的文件内容拷贝到新文件中
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				bisNurseProject.setCoverPhoto("/upload/coverPhoto/" + sdf.format(new Date()) + "/" + fileName1);
			}

			if (files[1].getOriginalFilename() != null && !files[1].getOriginalFilename().equals("")) {
				@SuppressWarnings("deprecation")
				String path1 = request.getRealPath("/upload/coverBigPhoto");// 获取项目的路径
				path1 = path1 + "/" + sdf.format(new Date());// 存放图片的路径
				File file5 = new File(path1);
				if (!file5.exists()) {// 如果当前项目不存在
					file5.mkdirs();// 创建一个新的空目录
				}
				String fileName2 = files[1].getOriginalFilename();
				File file4 = new File(path1, fileName2);// 第一个参数表示第二个参数所在的路径
				if (!file4.exists()) {// 如果当前项目不存在
					try {
						file4.createNewFile();// 创 建一个空的目录
						files[1].transferTo(file4);// 将上传的文件内容拷贝到新文件中
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				bisNurseProject.setCoverBigPhoto("/upload/coverBigPhoto/" + sdf.format(new Date()) + "/" + fileName2);
			}
		}
	}
}